package com.wbj.direction.sensor.direction

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.noboauto.vehicle.view.util.AltitudeUtil
import com.wbj.direction.Constant
import com.wbj.direction.R

/**
 * @auth: WANGCHUANGJUN
 * @mail: 910868438@qq.com
 * @desc:
 * @date: 2021/12/13 16:26
 */
class DirectionAdapter(var list: ArrayList<Int>?, var context: Context) :
    RecyclerView.Adapter<DirectionAdapter.DirectionViewHolder>() {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DirectionViewHolder {
        return DirectionViewHolder(
            LayoutInflater.from(context).inflate(R.layout.item_direction, parent, false), context)
    }

    override fun onBindViewHolder(holder: DirectionViewHolder, position: Int) {
        val angle: Int? = list?.get(position)
        holder.tvAngle.text = when (angle) {
            0 -> "北"
            45 -> "东北"
            90 -> "东"
            135 -> "东南"
            180 -> "南"
            225 -> "西南"
            270 -> "西"
            315 -> "西北"
            else -> angle.toString()
        }
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    private fun setData(
        holder: DirectionViewHolder,
        text: String,
        drawableId: Int,
        isNormal: Boolean,
    ) {
        holder.tvAngle.text = text
        holder.tvAngle.setTextColor(Color.WHITE)
    }

    override fun getItemCount(): Int {
        return if (null == list) 0 else list!!.size
    }

    class DirectionViewHolder(private val itemView: View, var context: Context) :
        RecyclerView.ViewHolder(itemView) {
        var tvAngle: TextView = itemView.findViewById(R.id.tvAngle)

        init {
            val param = itemView.layoutParams
            param.width = AltitudeUtil.dipToPx(context, Constant.compassSize)
            param.height = AltitudeUtil.dipToPx(context, Constant.compassSize)
            itemView.layoutParams = param
        }
    }
}